package com.example.blog.service.impl;

import com.example.blog.service.UploadService;
import com.example.blog.vo.Result;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Service
public class UploadServiceImpl implements UploadService {
    private String filePath = "/www/wwwroot/huamiaoyi.xyz/毕业设计/img/";
//    private String filePath ="/Users/huamiaoyi/Desktop/filetemp/";
    public Result imageUpload(MultipartFile file) throws IllegalStateException{
        File file1 = new File(filePath);
        if (!file1.exists()){
            file1.mkdirs();
        }
        //文件名称
        String realFileName = file.getOriginalFilename();
        //文件后缀
        String suffix = realFileName.substring(realFileName.lastIndexOf(".")+1);
        //文件处理
        String newFilename = UUID.randomUUID() + realFileName;
        //服务器返回名字就行
        String newFilePath = filePath + newFilename;
        //新文件的路径
        try {
            file.transferTo(new File(newFilePath));
        } catch (IllegalStateException e){
            e.printStackTrace();
        }catch (IOException e1) {
            e1.printStackTrace();
        }
        return Result.success("http://huamiaoyi.xyz/img/"+newFilename);
//        return Result.success(newFilePath);
    }
}
